let express = require('express')
let app = express()
let withList = []
app.use(function (req, res, next) {
    // let origin = req.origin
    // 允许那些源访问
    res.setHeader('Access-Control-Allow-origin', '*')
    // 允许那些方法请求
    res.setHeader('Access-Control-Allow-Mehtods', 'GET')
    // 允许的头部
    res.setHeader('Access-Control-Allow-Headers', 'name')
    // 是否允许携带cookie
    res.setHeader('Access-Control-Crdentials', true)
    // 允许跨域最长时间
    res.setHeader('Access-Control-Max-Age', 6)
    // 判断是否为options请求
    if (req.Headers.method === 'options') {
        res.end()
    }
    next()
})

app.get('/getData', function (req, res) {
    const data = {
        code: 200,
        list: [{
            id: 1,
            name: 'wzz'
        },
        {
            id: 2,
            name: 'csc'
        },
        {
            id: 1,
            name: 'wzz'
        },
        {
            id: 2,
            name: 'csc'
        },
        {
            id: 1,
            name: 'wzz'
        },
        {
            id: 2,
            name: 'csc'
        },
        {
            id: 1,
            name: 'wzz'
        },
        {
            id: 2,
            name: 'csc'
        },
        {
            id: 1,
            name: 'wzz'
        },
        {
            id: 2,
            name: 'csc'
        }]
    }
    res.send(data)
})
app.get('/getData1', function (req, res) {
    console.log(req.Headers);
    res.send('恩爱')
})
app.get('/getData2', function (req, res) {
    res.send('csc')
})
app.get('/getData3', function (req, res) {
    res.send('ym')
})

let server = app.listen(3000, function () {
    console.log('服务已启动');
})